package com.imd.s9.impl;

import com.alibaba.fastjson.JSONObject;
import com.imd.s9.entity.TableInfo;
import com.imd.s9.service.PurchaseService;
import com.imd.s9.util.InitCriteria;
import com.imd.utils.SequenceUtil;
import com.imd.vo.s3.UserVO;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class PurchaseImpl implements PurchaseService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private InitCriteria initCriteria;

    @Override
    public void createOrder(JSONObject doc,UserVO userVO) {
        List<LinkedHashMap> docs = doc.getObject("明细",List.class);
        Map<String,List<LinkedHashMap>> map = new HashMap<>();
        for(LinkedHashMap document:docs){
            String name = ObjectUtils.isEmpty(document.get("供应商"))?null:document.get("供应商").toString();
            if( ObjectUtils.isEmpty(name)){
                continue;
            }
            List<LinkedHashMap> temp = map.get(name)==null?new ArrayList<>():map.get(name);
            temp.add(document);
            map.put(name,temp);
        }
        for(String name:map.keySet()){
            List<LinkedHashMap> list = map.get(name);
            JSONObject document = new JSONObject();
            document.put("订单状态",false);
            document.put("单据编号", SequenceUtil.getSequence("CGDD"));
            document.put("项目名称",doc.getString("项目名称"));
            document.put("销售订单号",doc.get("销售订单号"));
//            Query query = initCriteria.createQuery(Criteria.where("供应商名称").is(name));
//            List<Document> lists = mongoTemplate.find(query, Document.class,TableInfo.supplier);
//            Document dc = new Document();
//            dc.put("id",lists.get(0).getObjectId("_id"));
//            dc.put("name",name);
            document.put("供应商",name);
            document.put("订单日期",doc.get("请购时间"));
            document.put("经手人",doc.get("经手人"));
            document.put("币种","RMB");
            document.put("明细",list);
            document.put("isDelete",0);
            document.put("tenantId", TableInfo.tenantId);
            document.put("createTime",new Date());
            document.put("createName",userVO.getName());
            HashMap map1 = new HashMap(document);
            mongoTemplate.save(map1, TableInfo.purchaseO);
        }
    }

    @Override
    public void createOrder1(JSONObject doc, UserVO userVO) {
        List<LinkedHashMap> docs = doc.getObject("明细",List.class);
        Map<String,List<LinkedHashMap>> map = new HashMap<>();
        for(LinkedHashMap document:docs){
            String name = ObjectUtils.isEmpty(document.get("外协厂家"))?null:((Map) document.get("外协厂家")).getOrDefault("供应商名称","").toString();
            if(ObjectUtils.isEmpty(name)){
                continue;
            }
            List<LinkedHashMap> temp = map.get(name)==null?new ArrayList<>():map.get(name);
            temp.add(document);
            map.put(name,temp);
        }
        for(String name:map.keySet()){
            List<LinkedHashMap> list = map.get(name);
            JSONObject document = new JSONObject();
            document.put("订单状态",false);
            document.put("单据编号", SequenceUtil.getSequence("WWDD"));
            document.put("项目名称",doc.getString("项目名称"));
            document.put("销售订单号","");
            document.put("供应商",name);
            document.put("订单日期",doc.get("请购时间"));
            document.put("经手人",doc.get("经手人"));
            Map bz = new HashMap(1);;
            bz.put("id",new ObjectId("66b6c3cc495f4c20811b5c9a"));
            document.put("币种",bz);
            document.put("附件",doc.get("委外要求"));
            document.put("明细",list);
            document.put("isDelete",0);
            document.put("tenantId", TableInfo.tenantId);
            document.put("createTime",new Date());
            document.put("createName",userVO.getName());
            HashMap map1 = new HashMap(document);
            mongoTemplate.save(map1, TableInfo.purchaseO);
        }
        Update up =new Update();
        up.set("是否执行",true);
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(doc.get("_id")));
        mongoTemplate.updateFirst(query,up,TableInfo.outPurchase);
    }

    @Override
    public void pushStorage(JSONObject doc, UserVO userVO) {
        JSONObject document = new JSONObject();
        document.put("订单状态",false);
        document.put("单据编号", SequenceUtil.getSequence("WWCK"));
        document.put("项目名称",doc.getString("项目名称"));
        document.put("经手人",doc.get("经手人"));
        document.put("币种","RMB");
        document.put("委外要求",doc.get("附件"));
        List<LinkedHashMap> docs = doc.getObject("明细",List.class);
        document.put("明细",docs);
        document.put("isDelete",0);
        document.put("tenantId", TableInfo.tenantId);
        document.put("createTime",new Date());
        document.put("createName",userVO.getName());
        document.put("外协厂家",doc.get("供应商"));
        HashMap map1 = new HashMap(document);
        mongoTemplate.save(map1, TableInfo.outsourceO);
        Update up =new Update();
        up.set("是否下发",true);
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(doc.get("_id")));
        mongoTemplate.updateFirst(query,up,TableInfo.purchaseO);
    }
}
